package com.roc.web.action.windcontroll;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.TransLimitBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblInsInf;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.service.riskRule.ITransLimitService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
@SuppressWarnings("serial")
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/translimit")
public class TransLimitAction extends PaginationBaseAction{
	private Logger logger=LogManager.getLogger(TransLimitAction.class);
	private TransLimitSearchCondition searchCondition;
	private List<TblInsInf> orgList=new ArrayList<TblInsInf>();
	private TransLimitBean bean;
	@Autowired
	IPageService pagenitionService;
	@Autowired
	IorgService orgService;
	@Autowired
	ITransLimitService transService;
	/**
	 * 交易限制初始化页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/windControllSta/translimit.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String init()throws Exception 
	{
		org();
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;
	}
	/**
	 * 交易限制列表页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "limitList", results = { @Result(name = "success", location = "/pages/windControllSta/translimit.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String transLimitView() throws Exception{
		try {			
			if(searchCondition==null){
				searchCondition=(TransLimitSearchCondition)getSearchConditionFromSession();
				if(searchCondition==null){
					searchCondition=new TransLimitSearchCondition();
				}
			}		
			//加载机构
			org();
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);				
				List<TransLimitBean> lists=new ArrayList<TransLimitBean>();
				if(searchRsList!=null&&searchRsList.size()>0)
				{
					Iterator<?> iterator=searchRsList.iterator();
					while(iterator.hasNext())
					{
						Object[] obj=(Object[]) iterator.next();
						TransLimitBean bean=new TransLimitBean();
						bean.setInCd(Constance.Null2EmptyTrim(obj[0]));
						bean.setMchtCd(Constance.Null2EmptyTrim(obj[1]));
						bean.setTermCd(Constance.Null2EmptyTrim(obj[2]));
						String transType=Constance.Null2EmptyTrim(obj[3]);
						bean.setTransType(transType.substring(4));//交易类型
						bean.setRiskInfo(Constance.Null2EmptyTrim(obj[4]));
						bean.setRiskCycle(Constance.Null2EmptyTrim(obj[5]));						
						bean.setRiskKey(Constance.Null2EmptyTrim(obj[6]));
						bean.setRiskMethod(Constance.Null2EmptyTrim(obj[7]));
						bean.setUnite(Constance.Null2EmptyTrim(obj[8]));
						bean.setValueMax(Constance.Null2EmptyTrim(obj[9]));
						bean.setStatus(Constance.Null2EmptyTrim(obj[10]));
						lists.add(bean);
					}
					searchRsList.clear();
					searchRsList.addAll(lists);
				}
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (Exception e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
	   return SUCCESS;
    }
	/**
	 * 初始化添加规则
	 * @return
	 */
	@Action(value = "addRuleJsp", results = { @Result(name = "success", location = "/pages/windControllSta/addRule.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String addRuleview()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			orgList=orgService.queryOrgList();
			bean=new TransLimitBean();
			bean.setValueMax("0");
			bean.setMchtCd("*");
			bean.setTermCd("*");
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;		
	}
	/**
	 * 添加规则(针对特定商户)
	 * @return
	 */
	@Action(value = "addRule", results = { @Result(name = "success", location = "/pages/windControllSta/translimit.jsp"),
			@Result(name = "submitRest", location = "/pages/windControllSta/addRule.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String addRule()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		try {
			Map<String, Object> map=transService.addNewRiskRuleToMcht(bean,op);
			if(map.containsKey("result"))
			{	
				orgList=orgService.queryOrgList();
				Object obj=map.get("result");
				addActionMessage(String.valueOf(obj));
				return "submitRest";
			}
			addActionMessage("添加成功!");
			org();
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("添加风控规则失败~");
		}
		return SUCCESS;		
	}
	/**
	 * 初始化修改规则
	 * @return
	 */
	@Action(value = "updateRuleJsp", results = { @Result(name = "success", location = "/pages/windControllSta/addRule.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String initRuleview()throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			orgList=orgService.queryOrgList();
			String infact_Type="POS_"+bean.getTransType();
			bean.setTransType(infact_Type);
			bean=transService.findBean(bean);
			getRequest().setAttribute("flag", "update");
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;		
	}
	/**
	 * 更改风控规则
	 * @return
	 * @throws Exception 
	 */
	@Action(value = "saveNewRiskRule", results = { @Result(name = "success", location="/pages/windControllSta/translimit.jsp"),
			@Result(name = "error", location="/error.jsp")})
	public String riskRule() throws Exception
	{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		try {
			int count=transService.updateRiskRule(bean, op);
			if(count==0)
			{
				addActionError("修改风控规则失败~");
				return ERROR;
			}
			addActionMessage("修改成功!");
			org();
		} catch (ServiceException e) {
			e.printStackTrace();
			addActionError("更改风控规则失败~");
		}
		return SUCCESS;
	}
	public void org()throws Exception{
		orgList=orgService.queryOrgList();
		TblInsInf t=new TblInsInf();
		t.setInsIdCd("*");
		t.setInsName("通用规则模板");
		orgList.add(t);
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		return null;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_TRANS_LIMIT_QUERY, searchCondition);		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_TRANS_LIMIT_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		return null;
	}
	public TransLimitSearchCondition getSearchCondition() {
		return searchCondition;
	}
	public void setSearchCondition(TransLimitSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	public List<TblInsInf> getOrgList() {
		return orgList;
	}
	public void setOrgList(List<TblInsInf> orgList) {
		this.orgList = orgList;
	}
	public TransLimitBean getBean() {
		return bean;
	}
	public void setBean(TransLimitBean bean) {
		this.bean = bean;
	}
	
}
